﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FieldDevice.UI
{
    public partial class TemperatureForm : DeviceFormBase
    {
        public TemperatureForm(Device device)
            :base(device)
        {
            InitializeComponent();
            this.Text = "Device:" + device.GetType().Name + "(address:" + device.ModbusAddress + ")";
            this.ReadChangeEventHander =
          new Device.ReadChangeEventHander(device_ReadChangeEvent);
            device.ReadChangeEvent += this.ReadChangeEventHander;
            UpdateReadsDelegate = new VoidDelegate(this.UpdateReads);
            this.TempTime.Enabled = false;
            Temper.Add("A相温度", 0.0f);
            Temper.Add("B相温度", 0.0f);
            Temper.Add("C相温度", 0.0f);

        }

        //private float Temper_A=0.0f;
        //private float Temper_B=0.0f;
        //private float Temper_C=0.0f;
        //private float Temper_D=0.0f;

        private bool WM_Control = false;                    //风机控制
        private bool OverTemper_Alarm = false;              //超温报警
        private bool Temperature_Exceed_Trip = false;       //超温跳闸
        private bool Slug_Temperature_Exceed = false;          //铁心超温
        private bool Fault_Alarm = false;               //故障报警
        private SortedList<string, float> Temper = new SortedList<string, float>();
        private 
 
        void device_ReadChangeEvent(Device device, string name, float value)
        {
            if (this.InvokeRequired)
                this.Invoke(this.UpdateReadsDelegate);
            else
                this.UpdateReads();
        }

        public override void UpdateReads()
        {
                Temper["A相温度"]=Device1.CurrentReads["Temper_A"];
                Temper["B相温度"]=Device1.CurrentReads["Temper_B"];
                Temper["C相温度"]=Device1.CurrentReads["Temper_C"];
                Fault.Alarm = Convert.ToBoolean(Device1.CurrentReads["Fault_Alarm"]);
                Temperature_Exceed.Alarm = Convert.ToBoolean(Device1.CurrentReads["OverTemperature_Alarm"]);

                WM_Control = Convert.ToBoolean(Device1.CurrentReads["WM_Control"]);
                Temperature_Exceed_Trip = Convert.ToBoolean(Device1.CurrentReads["OverTemperature_Trip"]);
                Slug_Temperature_Exceed = Convert.ToBoolean(Device1.CurrentReads["Slug_OverTemperature"]);

                AlarmList.Items.Clear();
                foreach (string v in Temper.Keys)
                {
                    if (Temper[v] == 0x7000)
                    {
                        ListViewItem list1 = new ListViewItem(v);
                        list1.SubItems.Add("开路");
                        AlarmList.Items.Add(list1);
                    }
                    else
                        if (Temper[v] == 0x6000)
                        {
                            ListViewItem list2 = new ListViewItem(v);
                            list2.SubItems.Add("超上限");
                            AlarmList.Items.Add(list2);
                        }
                        else
                            if (Temper[v] == 0x8000)
                            {
                                ListViewItem list3 = new ListViewItem(v);
                                list3.SubItems.Add("超下限");
                                AlarmList.Items.Add(list3);
                            }
                            else
                                if (Temper[v] == 0x5000)
                                {
                                    ListViewItem list4 = new ListViewItem(v);
                                    list4.SubItems.Add("故障");
                                    AlarmList.Items.Add(list4);
                                }
                                else
                                {
                                    if (v == "A相温度")
                                    {
                                        ListViewItem list5 = new ListViewItem(v);
                                        list5.SubItems.Add("正常");
                                        AlarmList.Items.Add(list5);

                                        this.Thermometer_A.Temper_Value =Temper[v];
                                        this.tempA.Text = this.Thermometer_A.Temper_Value.ToString("F1")+"℃";
  
                                    }
                                    else
                                    if (v == "B相温度")
                                    {
                                        ListViewItem list6 = new ListViewItem(v);
                                        list6.SubItems.Add("正常");
                                        AlarmList.Items.Add(list6);

                                        this.Thermometer_B.Temper_Value = Temper[v];
                                        this.tempB.Text = this.Thermometer_B.Temper_Value.ToString("F1") + "℃";
                                    }
                                    else
                                    if (v == "C相温度")
                                    {
                                        ListViewItem list7 = new ListViewItem(v);
                                        list7.SubItems.Add("正常");
                                        AlarmList.Items.Add(list7);

                                        this.Thermometer_C.Temper_Value = Temper[v];
                                        this.tempC.Text = this.Thermometer_C.Temper_Value.ToString("F1") + "℃";  
                                    }                                 
                                }

                }

                if (WM_Control)
                {
                    ListViewItem list8 = new ListViewItem("风机控制");
                    list8.SubItems.Add("开启");
                    AlarmList.Items.Add(list8);
                }
                else
                {
                    ListViewItem list8 = new ListViewItem("风机控制");
                    list8.SubItems.Add("停止");
                    AlarmList.Items.Add(list8);
                }

                if (Temperature_Exceed_Trip)
                {
                    ListViewItem list9 = new ListViewItem("超温跳闸");
                    list9.SubItems.Add("跳闸");
                    AlarmList.Items.Add(list9);
                }
                if (Slug_Temperature_Exceed)
                {
                    ListViewItem list10 = new ListViewItem("铁心超温状态");
                    list10.SubItems.Add("超温");
                    AlarmList.Items.Add(list10);
                }

        }

        private void buttonUpdateReads_Click(object sender, EventArgs e)
        {
            try
            {
                this.Device1.DebugReads();
            }
            catch (Exception exp)
            {
                FieldLib.ExceptionHandler.OnException(this, exp);
            }
            this.UpdateReads();
        }

        private void TempTime_Tick(object sender, EventArgs e)
        {
            try
            {
                this.Device1.DebugReads();
            }
            catch (Exception exp)
            {
                FieldLib.ExceptionHandler.OnException(this, exp);
            }
            this.UpdateReads();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.TempTime.Enabled = this.CheckBox.Checked;
        }

    }
}
